#include "stdafx.h"
/*
#include "SoundPlayer.h"

SoundPlayer * SoundPlayer::theInstance = 0;

SoundPlayer::SoundPlayer()
{
	SoundPlayer::initialize();
	m_promptMap = new std::map<int, FMOD::MusicPrompt*>();
	m_eventMap = new std::map<int, FMOD::Event*>();
}
SoundPlayer::~SoundPlayer()
{
	delete m_promptMap;
	delete m_eventMap;
	m_promptMap = NULL;
	m_eventMap = NULL;
	//TODO
	//FMOD::EventSystem	*m_system;
	delete m_musicSystem;
	m_musicSystem = NULL;

	//result = FMOD_Sound_Release(sound1);
	//ERRCHECK(result);
	//	result = FMOD_Sound_Release(sound2);
	//ERRCHECK(result);
	//result = FMOD_Sound_Release(sound3);
	//ERRCHECK(result);


	//m_result = FMOD::EventSystem_Close(&m_system);
	//errcheck(m_result);
			
	//m_result = FMOD::EventSystem_Release(&m_system);
	//errcheck(m_result);
}
SoundPlayer *SoundPlayer::getInstance()
{
  if (!theInstance)
  {
	 theInstance = new SoundPlayer();
  }
  return theInstance;
}

void SoundPlayer::destroyInstance()
{
	if(theInstance)
	{
		delete theInstance;
		theInstance = NULL;
		

		
	}
}
void changeMusicParameter(unsigned int paramId, float paramValue)
{


}

void SoundPlayer::activateCue(unsigned int cueId)
{
	//TODO refactor into function
	FMOD::MusicPrompt    *prompt = 0;
	if(m_promptMap->find(cueId) != m_promptMap->end())
	{
		prompt = (*m_promptMap)[cueId];
	}
	if(!prompt)
	{
		m_result = m_musicSystem->prepareCue(cueId,    &prompt);
		errcheck(m_result);
		(*m_promptMap)[cueId] = prompt;

	}
	bool active;
	m_result = prompt->isActive(&active);
	errcheck(m_result);


	
    if (!active)
    {
		m_result = prompt->begin();
	    errcheck(m_result);
	}
}
void SoundPlayer::changeMusicParameter(unsigned int paramId, float paramValue)
{

	m_result = m_musicSystem->setParameterValue(paramId, paramValue);
	errcheck(m_result);
}
void SoundPlayer::deactivateCue(unsigned int cueId)
{
	FMOD::MusicPrompt    *prompt;
	if(m_promptMap->find(cueId) != m_promptMap->end())
	{
		prompt = (*m_promptMap)[cueId];
	}
	if(!prompt)
	{
		m_result = m_musicSystem->prepareCue(cueId,    &prompt);
		errcheck(m_result);
		(*m_promptMap)[cueId] = prompt;

	}
	bool active;
	m_result = prompt->isActive(&active);
	errcheck(m_result);

	
    if (active)
    {
		m_result = prompt->end();
	    errcheck(m_result);
	}
  
}

void SoundPlayer::quickCue(unsigned int cueId)
{
	FMOD::MusicPrompt    *prompt;
	if(m_promptMap->find(cueId) != m_promptMap->end())
	{
		prompt = (*m_promptMap)[cueId];
	}
	if(!prompt)
	{
		m_result = m_musicSystem->prepareCue(cueId,    &prompt);
		errcheck(m_result);
		(*m_promptMap)[cueId] = prompt;

	}
	bool active;
	m_result = prompt->isActive(&active);
	errcheck(m_result);

	
    if (active)
    {
		m_result = prompt->end();
	    errcheck(m_result);
		
	}
	m_result = prompt->begin();
	errcheck(m_result);
	m_result = prompt->end();
	errcheck(m_result);

}

void SoundPlayer::play2dSound(const char *soundName)
{
	//FMOD::Channel     *channel = 0;
	//m_result = m_system->playSound(FMOD_CHANNEL_FREE, m_sound, false, &channel);
   // errcheck(m_result);


}
void SoundPlayer::createSound(const char *path, const char *soundName)
{
	//FMOD::Sound *sound;
	//m_result = m_system->createSound(path,FMOD_SOFTWARE, 0, &sound);
	//dictionary[*soundName] = sound;
    //errcheck(m_result);

}

void SoundPlayer::update()
{
	m_result = m_system->update();
	errcheck(m_result);

}
int SoundPlayer::initialize()
{
	

	m_result = FMOD::EventSystem_Create(&m_system);
	errcheck(m_result);

	


   
	m_result = m_system->getVersion(&m_version);
    errcheck(m_result);

    if (m_version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", m_version, FMOD_VERSION);
        return 0;
    }

	m_result = m_system->init(256, FMOD_INIT_NORMAL, 0);
	errcheck(m_result);

	m_result = m_system->setMediaPath("..\\media\\");
	errcheck(m_result);

	m_result = m_system->load("fmodproj.fev", 0, 0);
	errcheck(m_result);

	m_result = m_system->getMusicSystem(&m_musicSystem);
	errcheck(m_result);




    
	//activateCue(MUSICCUE_FMODPROJ_START);
	//m_result = evnt->start();
	return 0;



	

}

void SoundPlayer::activateEvent(unsigned int eventId)
{
	FMOD::Event    *evt = 0;
	if(m_eventMap->find(eventId) != m_eventMap->end())
	{
		evt = (*m_eventMap)[eventId];
	}
	if(!evt)
	{
		m_result = m_system->getEventBySystemID(eventId, FMOD_EVENT_DEFAULT, &evt);
		errcheck(m_result);
		(*m_eventMap)[eventId] = evt;

	}
	m_result = evt->start();
	errcheck(m_result);


		
    


}
void SoundPlayer::deactivateEvent(unsigned int eventId)
{
	FMOD::Event    *evt = 0;
	if(m_eventMap->find(eventId) != m_eventMap->end())
	{
		evt = (*m_eventMap)[eventId];
	}
	if(!evt)
	{
		m_result = m_system->getEventBySystemID(eventId, FMOD_EVENT_DEFAULT, &evt);
		errcheck(m_result);
		(*m_eventMap)[eventId] = evt;

	}
	m_result = evt->stop();
	errcheck(m_result);

}

void SoundPlayer::changeEventParameter(unsigned int eventId, const char* paramName, float paramValue)
{
	//TODO refactor into method
	FMOD::Event    *evt;
	if(m_eventMap->find(eventId) != m_eventMap->end())
	{
		evt = (*m_eventMap)[eventId];
	}
	if(!evt)
	{
		m_result = m_system->getEventBySystemID(eventId, FMOD_EVENT_DEFAULT, &evt);
		errcheck(m_result);
		(*m_eventMap)[eventId] = evt;

	}
	FMOD::EventParameter* param;
	m_result = evt->getParameter(paramName, &param);
    m_result = param->setValue(paramValue);
}

void SoundPlayer::errcheck(FMOD_RESULT result)
{
	if (result != FMOD_OK)
    {
        printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
        exit(-1);
    }
}

*/

//more complex init, may be needed on some PCs
/*
    m_result = FMOD::System_Create(&m_system);
    errcheck(m_result);
   
    m_result = m_system->getVersion(&m_version);
    errcheck(m_result);
	
    if (m_version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", m_version, FMOD_VERSION);
        return 0;
    }
    int  numdrivers;
    m_result = m_system->getNumDrivers(&numdrivers);
    errcheck(m_result);

    m_result = m_system->setHardwareChannels(0);  // Skip EAX probe which can crash/corrupt some bad drivers.
    errcheck(m_result);
	
    if (numdrivers == 0)
    {
        m_result = m_system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
        errcheck(m_result);
    }
    else
    {
		FMOD_CAPS        caps;
		FMOD_SPEAKERMODE speakermode;

        m_result = m_system->getDriverCaps(0, &caps, 0, &speakermode);
        errcheck(m_result);

        m_result = m_system->setSpeakerMode(speakermode);       // Set the user selected speaker mode.
        errcheck(m_result);
		
        if (caps & FMOD_CAPS_HARDWARE_EMULATED)             // The user has the 'Acceleration' slider set to off!  This is really bad for latency!.
        {                                                   // You might want to warn the user about this. 
            m_result = m_system->setDSPBufferSize(1024, 10);
            errcheck(m_result);
        }

		char             name[256];

        m_result = m_system->getDriverInfo(0, name, 256, 0);
        errcheck(m_result);

        if (strstr(name, "SigmaTel"))   // Sigmatel sound devices crackle for some reason if the format is PCM 16bit.  PCM floating point output seems to solve it.
        {
            m_result = m_system->setSoftwareFormat(48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
            errcheck(m_result);
        }
    }
	
    m_result = m_system->init(100, FMOD_INIT_NORMAL, 0);
    if (m_result == FMOD_ERR_OUTPUT_CREATEBUFFER)         // Ok, the speaker mode selected isn't supported by this soundcard.  Switch it back to stereo... 
    {
        m_result = m_system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
        errcheck(m_result);
           
        m_result = m_system->init(100, FMOD_INIT_NORMAL, 0);// ... and re-init. 
        if (m_result == FMOD_ERR_OUTPUT_CREATEBUFFER)         // Now the directsound driver can't even create a stereo buffer - great, let's try WINMM ... 
        {
            m_result = m_system->setOutput(FMOD_OUTPUTTYPE_WINMM);
            errcheck(m_result);
           
            m_result = m_system->init(100, FMOD_INIT_NORMAL, 0);// ... and re-init. 
            errcheck(m_result);
        }
    }
	*/